package br.com.jk.master.processos;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Component;
import javax.swing.BorderFactory;
import javax.swing.JPanel;
import javax.swing.JProgressBar;
import javax.swing.JTable;
import javax.swing.SwingUtilities;
import javax.swing.table.DefaultTableCellRenderer;
import javax.swing.table.TableCellRenderer;

/**
 *
 * @author camilo
 */
public class ProcessoTableCellRenderer extends DefaultTableCellRenderer implements TableCellRenderer{

  public static final int PROGRES_INDEX_MAXIMUM = 1;
  public static final int PROGRES_INDEX_VALUE = 0;
  private static final long serialVersionUID = 1L;
  private final JProgressBar jProgressBar = new JProgressBar(0, 0);
  private final JPanel jPanel = new JPanel(new BorderLayout());

  public ProcessoTableCellRenderer() {
    super();
//    setOpaque(true);
    jPanel.add(jProgressBar);
  }

  @Override
  public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column) {
    Integer[] i = (Integer[]) value;
    String text = "Done";
    jProgressBar.setMaximum(i[PROGRES_INDEX_MAXIMUM]);
    
    if (i.length < 0) {
      text = "Canceled";
    } else if (i[PROGRES_INDEX_VALUE] < i[PROGRES_INDEX_MAXIMUM]) {
      jProgressBar.setValue(i[PROGRES_INDEX_VALUE]);
      return jPanel;
    }
    table.setBackground(Color.red);
    super.getTableCellRendererComponent(table, text, isSelected, hasFocus, row, column);
    return this;
  }

  @Override
  public void updateUI() {
    super.updateUI();
    if (jPanel != null) {
      SwingUtilities.updateComponentTreeUI(jPanel);
    }
  }
}
